﻿#include "tools.h"
#include <QDir>
#include <QProcess>
#include "log.h"
#include <QCryptographicHash>
#include <QtDebug>
#include<QCoreApplication>
QString Tools::mkMutiDir(const QString path){
    QDir dir(path);
    if ( dir.exists(path)){
        return path;
    }
    QString parentDir = mkMutiDir(path.mid(0,path.lastIndexOf('/')));
    QString dirname = path.mid(path.lastIndexOf('/') + 1);
    QDir parentPath(parentDir);
    if ( !dirname.isEmpty() )
        parentPath.mkpath(dirname);
    return parentDir + "/" + dirname;
}

void Tools::findAllfile(const QString &floder, QStringList &result,bool isMainThread)
{
    if(isMainThread){
        QCoreApplication::processEvents();
    }

    QDir dir(floder);
    auto dirPath = QDir::fromNativeSeparators(floder);
    foreach(auto fileName, dir.entryList(QDir::Files)) {
        result.append(floder+"/"+fileName);
    }
    foreach(auto dirName,dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot)){
        findAllfile(floder+"/"+ dirName,result);
    }
}

QString Tools::runCommand(const QString &command)
{
    try
    {
        FILE* fp = _popen(command.toStdString().c_str(), "r");
        if (fp != nullptr)
        {
            QString result;
            char buf[1024] = { 0 };
            while (fgets(buf, sizeof(buf), fp))
            {
                result.append(buf);
            }
            _pclose(fp);
            return result;
        }
        throw "system run";
    }
    catch (...)
    {
        return "warning system run command fail\n";
    }
}

void Tools::openExplorerFile(const QString &path)
{
    //打开资源管理器并高亮文件
    const QString explorer = "explorer";
    QStringList param;
    if(!QFileInfo(path).isDir()){
        param<<QLatin1String("/select,");
    }
    param<<QDir::toNativeSeparators(path);
    QProcess::startDetached(explorer,param);
}

void Tools::createMklink(const QString &folder, const QString &folderName, const QString &targetFolder)
{
    QDir dir(QString("%1/%2").arg(folder).arg(folderName));
    if(dir.exists())
        return;
    auto tmp = targetFolder;
    auto cmd= QString("cd %1 & mklink /D %2 %3").arg(folder).arg(folderName).arg(tmp.replace("/","\\"));
    Log::Debug(cmd);
    system(cmd.toStdString().c_str());
}

QString Tools::GetFileMD5(QString filePath)
{
    QFile file(filePath);
    QString md5 = "";
    if(file.open(QIODevice::ReadOnly))
    {
        QByteArray array = QCryptographicHash::hash(file.readAll(), QCryptographicHash::Md5).toHex();
        file.close();
        md5.append(array);
    }
    return md5;
}

void Tools::deleteDir(const QString &path)
{
    if(path.isEmpty()|| !QDir().exists(path))
        return;

    QFileInfo info(path);
    if(info.isFile())
    {
        QFile::remove(path);
    }
    else if(info.isDir()){
        QDir dir(path);
        dir.setFilter(QDir::AllEntries|QDir::NoDotAndDotDot);
        QCoreApplication::processEvents();
        foreach(auto &fileInfo,dir.entryInfoList())
        {
            if(fileInfo.isFile())
            {
                dir.remove(fileInfo.absoluteFilePath());
            }else{
                deleteDir(fileInfo.absoluteFilePath());
                dir.rmdir(fileInfo.absoluteFilePath());
            }
        }
        dir.rmdir(info.absoluteFilePath());
    }
}
